package com.example.bus.controller;

import com.example.bus.entity.UserEntity;
import com.example.bus.google.event.EventBusListener1;
import com.example.bus.google.event.EventBusListener2;
import com.example.bus.spring.bus.DemoBus;
import com.google.common.eventbus.EventBus;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author guoQ.z
 * @date 2021/3/3
 */
@Slf4j
@RestController
@RequestMapping("/bus")
public class BusController {

    @Autowired
    private EventBus eventBus;
    @Autowired
    private ApplicationContext applicationContext;


    @ApiOperation("发送Bus消息")
    @GetMapping("/sendMsg")
    public void sendMes(String msg) {
        EventBusListener1 eventBusListener1 = new EventBusListener1();
        eventBusListener1.setInfo("haha");
        eventBus.post(eventBusListener1);

        EventBusListener2 listener2 = new EventBusListener2();
        listener2.setInfo("张国庆");
        eventBus.post(listener2);
    }

    @GetMapping("/sendSpringMes")
    public void sendSpringMes() {
        applicationContext.publishEvent(new DemoBus(this, new UserEntity().setId("id123").setName("name321")));
    }
}
